<template>
  <div class="box">
    <img id="logo" src="@/assets/images/logo.png" alt="电商ERP就选吉客云"  class="logo_img">

    <div class="background">
      <div class="background-content">
        <img id="logo" src="@/assets/images/login_bg.png" alt="电商ERP就选吉客云" style="object-fit: cover;width: 100%;">

      </div>
    </div>

    <div class="login-section">
      <div class="top_form">

        <div class="login-container">
          <div class="logo">
            <!--                    <img id="logo" src="../../assets/logo.png" alt="电商ERP就选吉客云" style="width: 100px;height: 100px;">-->
          </div>

          <h2>用户登录</h2>

          <form >
<!--            <div class="form-group">-->
<!--              <label>系统账号</label>-->
<!--              <input type="text" class="ipt" v-model="loginForm.JkNumber" placeholder="请输入系统账号：">-->
<!--            </div>-->

            <div class="form-group">
              <label>账号</label>
              <input type="text" class="ipt" v-model="loginForm.username" placeholder="请输入账号" >
            </div>

            <div class="form-group" >
              <label>密码</label>
              <div style="position: relative;">
                <input :type="secret?'password':'text'" class="passWord" v-model="loginForm.password" placeholder="请输入密码：" >
                <i style="font-size: 24px;position: absolute;right:10px;top: 10px;color: #d1d5d3" :class="[ secret ?'el-icon-lock':'el-icon-unlock']" @click="secret = !secret"></i>
              </div>
            </div>
            <div class="form-group">
              <label>验证码</label>
              <div class="code_img">
                <input type="text" class="passWord"   style="width: 63%" v-model="loginForm.code" placeholder="请输入密码：" >
                <img :src="codeUrl" @click="getCode" class="login-code-img"/>
              </div>
            </div>
            <button class="login-btn"  @click="handleLogin($event)">登 录</button>
          </form>
        </div>
      </div>
      <div class="footer">
        <p class="tech-support">技术支持：常州慧锦网络科技有限公司</p>
        <p>邓经理：18151991375</p>
        <p>苏ICP备2025172255号-2</p>
      </div>
    </div>


    <div
      class="code_fixed"
      :style="{ right: isExpanded ? '0px' : '-100px'}"
    >
      <div
        class="arrow-container"
        @click="toggleExpand"
      >
        <div class="arrow_left">
          <i class="el-icon-d-arrow-left"  :style="{ transform: isExpanded ? 'rotate(180deg)' : 'rotate(0deg)' }"></i>
        </div>
      </div>
      <div v-if="isExpanded" class="image-container">
          <el-image
            :src="imgUrl"
            fit="cover"
            style="width: 100px;height: 100px;"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
      </div>
    </div>
  </div>

</template>

<script>
import imgUrl from "@/assets/images/good.png"
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      imgUrl,
      secret:true,
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        // username: "admin",
        // password: "admin123",
        // JkNumber:'22729',
        // username: "729551527",
        // password: "123456",
        // JkNumber:'729551527',
        username: "",
        password: "",
        // JkNumber:'',
        rememberMe: false,
        code: "",
        uuid: ""
      },
      isExpanded: false,
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    toggleExpand() {
      this.isExpanded = !this.isExpanded
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      event.preventDefault();
      // if (!this.loginForm.JkNumber || this.loginForm.JkNumber.trim() == '') {
      //   this.$message.error('请输入系统账号')
      //   return
      // }
      if ( !this.loginForm.username || this.loginForm.username.trim() == '') {
        this.$message.error('请输入账户')
        return
      }
      if ( !this.loginForm.password || this.loginForm.password.trim() == '') {
        this.$message.error('请输入密码')
        return
      }
      // this.$refs.loginForm.validate(valid => {
      //   if (valid) {
      this.loading = true;
      if (this.loginForm.rememberMe) {
        Cookies.set("username", this.loginForm.username, { expires: 30 });
        Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
        Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
      } else {
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove('rememberMe');
      }
      this.$store.dispatch("Login", this.loginForm).then(() => {
        this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
      }).catch(() => {
        this.loading = false;
        if (this.captchaEnabled) {
          this.getCode();
        }
        // });
        // }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}

.box {
  overflow: hidden;
  min-height: 100vh;
  //background: #cccccc;
  background: linear-gradient(135deg, #1890ff, #096dd9);
  display: flex;
}

.background {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  padding: 40px;
}

.background-content {
  max-width: 700px;
}
.top_form{
  flex: 1;
  display: flex;
  align-content: center;
  justify-content: center;
  align-items: center;
}
.background h1 {
  font-size: 36px;
  margin-bottom: 20px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.background p {
  font-size: 16px;
  line-height: 1.6;
  opacity: 0.9;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.login-section {
  width: 480px;
  display: flex;
  padding: 40px;
  background-color: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-left: 1px solid rgba(255, 255, 255, 0.2);
  flex-direction: column;
}

.login-container {
  width: 100%;
  max-width: 380px;
  color: white;
}

.logo {
  text-align: center;
  margin-bottom: 30px;
}

.logo img {
  height: 50px;
  filter: brightness(0) invert(1);
}

h2 {
  text-align: center;
  color: white;
  margin-bottom: 30px;
  font-size: 24px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

input {
  width: 100%;
  padding: 12px 15px;
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 4px;
  box-sizing: border-box;
  font-size: 14px;
  transition: all 0.3s;
  color: white;
}

input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

input:focus {
  border-color: rgba(255, 255, 255, 0.8);
  outline: none;
  background-color: rgba(255, 255, 255, 0.3);
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: rgba(255, 255, 255, 0.9);
  color: #000000;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  font-weight: 600;
  transition: all 0.3s;
  margin-top: 10px;
}

.login-btn:hover {
  background-color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.footer {
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  font-size: 12px;
  margin-top: 30px;
}

.tech-support {
  color: rgba(255, 255, 255, 0.8);
  font-size: 13px;
}

button {
  padding: 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

/*.ipt::placeholder{*/
/*    text-align: center;*/
/*}*/
.logo_img{
  position: fixed;
  top: 20px;
  left: 20px;
  width: 300px;
}
.code_img{
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.code_fixed {
  width: 130px;
  height: 130px;
  position: fixed;
  bottom: 15px;
  background-color:rgb(36, 100, 171);
  display: flex;
  justify-content: space-between;
  transition: all 0.3s ease;
  border-top-left-radius: 0.375rem;
  border-bottom-left-radius: 0.375rem;
  .arrow-container {
    display: flex;align-items: center;
  }
  .arrow_left{
    width: 30px;
    display: flex;align-items: center;justify-content: center;
  }

  .image-container {
    display: flex;
    align-items: center;
  }
}
</style>
